<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class OrderrestController extends Uuch_RestController {
    public function indexAction() {
    }
    
    public function getAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $input = file_get_contents("php://input");
        $params = Zend_Json::decode($input);
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            $data = Zend_Json::encode ( $ifGoOn );
            echo $data;
            return;
        }
        if(!method_exists($this,$params["functionName"])){
            return array("Result"=>1006,"ResultMessage"=>"所需要的服务不存在或者暂不可用");
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        return;
    }
    
    public function postAction() {
        return false;
    }
    
    public function putAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $input = file_get_contents("php://input");
        $params = Zend_Json::decode($input);
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            $data = Zend_Json::encode ( $ifGoOn );
            echo $data;
            exit();
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params["params"]);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        return;
    }
    
    public function deleteAction() {
        return false;
    }
    
     /**
     * @return 获取个人代购详情
     */
    public function OrderInfo($params) {
        $isAuth = $this->analysisAuth($params["head"]);
        if($isAuth["Result"] != 1) {
            return $isAuth;
        }
        $storeItemSearch = new Application_Model_Filter_OrderSearch();
        $storeItemSearch->userid = $isAuth["userId"];
        $pageindex = $params["idx"]?($params["idx"]==1?0:($params["idx"]-1)*10):0;
        $orderItem = Application_Model_Order::getByPages($storeItemSearch, 10, $pageindex);
        $returnData = array();
        if($orderItem){
            $returnData = $this->analysisOrder($orderItem);
        }
        $returnData["Result"] = 0;
        $returnData["ResultMessage"] = "获取成功";
        $returnData["auth"] = Uuch_Util::encryptString($isAuth["userId"]."&".date("Y-m-d H:i:s"));
        return $returnData;
    }
    /**
     * @return 解析返回代购订单详情所需数据
     */
    protected function analysisOrder($orderItem) {
        $returnData = array();
        $noPayNum = 0;
        $i = 0;
        foreach ($orderItem["list"] as $orderVal) {
            $returnData["orderInfo"][$i]["orderId"] = $orderVal->Id;
            $returnData["orderInfo"][$i]["orderNo"] = $orderVal->OrderNum;
            $returnData["orderInfo"][$i]["orderCreateDate"] = $orderVal->DateCreated;
            $returnData["orderInfo"][$i]["orderLogisticsName"] = $orderVal->logisticsBy->Name;
            $returnData["orderInfo"][$i]["orderLogistics"] = $orderVal->LogisticsNo;
            $returnData["orderInfo"][$i]["orderProductTotal"] = $orderVal->ProductTotal;
            $returnData["orderInfo"][$i]["orderShipFee"] = $orderVal->ShipFee;
            $returnData["orderInfo"][$i]["orderSum"] = $orderVal->Sum;
            $returnData["orderInfo"][$i]["orderDiscount"] = $orderVal->Discount;
            $returnData["orderInfo"][$i]["orderProcureNo"] = $orderVal->ProcureNo;
            $returnData["orderInfo"][$i]["orderStatusName"] = $orderVal->StatusText;
            $returnData["orderInfo"][$i]["orderStatus"] = $orderVal->Status;
            $returnData["orderInfo"][$i]["orderItemInfo"] = $this->analysisOrderItem($orderVal->Id);
            if($orderVal->Status == Application_Model_Order::STATUS_PAYING) {
                $noPayNum++;
            }
            $i++;
        }
        $returnData["count"] = $orderItem["size"];
        $returnData["pages"] = $orderItem["pages"];
        $returnData["noPayNum"] = $noPayNum;
        return $returnData;
    }
    /**
     * @return 解析返回代购订单商品详情所需数据
     */
    protected function analysisOrderItem($orderId) {
        $orderItem = Application_Model_OrderItem::getByOrderId($orderId);
        $returnOrderInfo = array();
        if(!$orderItem){
            return $returnOrderInfo;
        }
        $i = 0;
        foreach ($orderItem as $itemVal) {
            $returnOrderInfo[$i]["itemId"] = $itemVal->Id;
            $returnOrderInfo[$i]["itemProductName"] = $itemVal->ProductName;
            $returnOrderInfo[$i]["itemPrice"] = $itemVal->price;
            $returnOrderInfo[$i]["itemQuantity"] = $itemVal->quantity;
            $returnOrderInfo[$i]["itemImage"] = urlencode($itemVal->Image);
            $returnOrderInfo[$i]["itemUrl"] = urlencode($itemVal->Product->Url);
            $returnOrderInfo[$i]["itemPropertyAlias"] = urlencode($itemVal->Product->PropertyAlias);
            $returnOrderInfo[$i]["itemComment"] = $itemVal->Comment;
            $returnOrderInfo[$i]["itemDescription"] = $itemVal->Description;
            $returnOrderInfo[$i]["itemAbnormal"] = $itemVal->StatusText;
            $returnOrderInfo[$i]["itemScheme"] = $itemVal->StatusName;
            $i++;
        }
        return $returnOrderInfo;
    }
}